package helper

import (
	"fmt"
	"strconv"
	"strings"

	"github.com/ant0ine/go-json-rest/rest"
)

func GetIp(r *rest.Request) (string, int) {
	ips := r.PathParam("id")
	ipa := strings.Split(ips, ":")
	port := 0
	if len(ipa) == 2 {
		port, _ = strconv.Atoi(ipa[1])
	}
	return strings.Join(strings.Split(ipa[0], "-"), "."), port
}

func GetId(ip string, port int) string {
	if port <= 0 {
		return ip
	}
	return fmt.Sprintf("%s:%d", ip, port)
}

func Rsuccess(msg string) map[string]string {
	data := map[string]string{}
	data["code"] = "0"
	data["message"] = msg
	return data
}

func Rerror(msg string) map[string]string {
	data := map[string]string{}
	data["code"] = "1"
	data["message"] = msg
	return data
}
